//
//  ViewController.swift
//  TextKitDemo
//
//  Created by ZJ on 2018/7/16.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        setupGUI()
//        linkAttribute()
    }

    private func linkAttribute() {
        guard let url = NSURL(string: "http://www.jianshu.com") else {
            return
        }
        
        let attributes = [NSAttributedStringKey.link: url]
//        let attrStr = NSAttributedString(string: "简书", attributes: attributes)
        let str = "跳转到简书"
        
        let attrStr = NSMutableAttributedString(string: str)
        attrStr.addAttributes(attributes, range: (str as NSString).range(of: "简书"))
        
        let rect = CGRect(x: 0, y: 0, width: 200, height: 200);
        
        let label = UITextView(frame: rect)
        label.center = view.center
        label.attributedText = attrStr
        label.isEditable = false
        label.delegate = self
        label.font = UIFont.systemFont(ofSize: 16)
        
        view.addSubview(label)
    }
    
    private func setupGUI() {
        let rect = CGRect(x: 0, y: 0, width: 200, height: 21);

        let label = ZJLabel(frame: rect)
        label.center = view.center
        label.numberOfLines = 0

        label.text = "@百度 😄哈哈 https://www.baidu.com"
        label.textColor = UIColor.gray

//        let attrText = NSMutableAttributedString(string: "@百度😄哈哈https://www.baidu.com")
//        attrText.addAttributes([(kCTFontAttributeName as NSAttributedStringKey): UIFont.systemFont(ofSize: 16),
//                                  (kCTForegroundColorAttributeName as NSAttributedStringKey): UIColor.darkGray],
//                                 range: NSRange(location: 0, length: attrText.length))
//        label.attributedText = attrText

        label.delegate = self

        view.addSubview(label)
        
//        let label = SoolyLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
//        label.text = "@百度 😄哈哈 https://www.baidu.com" //"Swift#iOS# 测试 http://www.sina.com"
////        label.font = UIFont.systemFont(ofSize: 12)
//        label.textColor = UIColor.gray
//        label.center = view.center
//        label.numberOfLines = 0
//
//        view.addSubview(label)
//
//        label.delegate = self
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController: SoolyLabelDelegate {
    func labelDidSelectedAt(text: String) {
        print("点击了\(text)用户")
    }
    
    func labelDidSelectedLink(text: String) {
        print("点击了\(text)链接")
    }
    
    func labelDidSelectedTopic(text: String) {
        print("点击了\(text)话题")
    }
}

extension ViewController: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        print("点击字符串: \(text)")
        // 判断是否是 URL
        if !text.hasPrefix("http://") {
            return
        }
        
        // 插入 ? 表示如果代理没有实现协议方法，就什么都不做
        // 如果使用 !，代理没有实现协议方法，仍然强行执行，会崩溃！
    }
}

extension ViewController: ZJLabelDelegate {
    func labelDidSelectedLinkText(_ label: ZJLabel!, text: String!) {
        print("点击字符串: \(text ?? "")")
    }
}

extension ViewController: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        print("url: \(URL)")
        return true;
    }
}
